package com.qf.news.exception;

import com.qf.news.entity.R;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 全局异常处理
     * @param e
     */
    @ExceptionHandler(Exception.class)
    public R globalException(Exception e){
        //记录日志
        e.printStackTrace();
        return R.fail(500, "服务器异常...");
    }

    /**
     * 自定义异常的处理器
     * @return
     */
    @ExceptionHandler(ServiceException.class)
    public R serviceException(ServiceException e){
        Integer code = e.getCode();
        String msg = e.getMsg();
        return R.fail(code, msg);
    }
}
